﻿using System;
using System.Security.Principal;
using EPiServer.Core;
using EPiServer.Security;

namespace EPiSugar.Navigation
{
    public class Filter
    {
        public static Filter By
        {
            get { return new Filter(); }
        }

        public Func<PageData, bool> PageType(int pageTypeID)
        {
            return x => x.PageTypeID == pageTypeID;
        }

        public Func<PageData, bool> PageType(string pageTypeName)
        {
            return x => x.PageTypeName == pageTypeName;
        }

        public Func<PageData, bool> Parent(PageReference parentLink)
        {
            return x => x.ParentLink == parentLink;
        }

        public Func<PageData, bool> Parent(PageData parent)
        {
            return x => x.ParentLink == parent.PageLink;
        }

        public Func<PageData, bool> Visible()
        {
            return x => x.VisibleInMenu;
        }

        public Func<PageData, bool> NotVisible()
        {
            return x => !x.VisibleInMenu;
        }

        public static Func<DateTime> CurrentTime = () => DateTime.Now;

        public Func<PageData, bool> Published()
        {
            return x => x.StartPublish <= CurrentTime() && CurrentTime() < x.StopPublish;
        }

        public Func<PageData, bool> Access(IPrincipal user)
        {
            return x => x.GetSecurityDescriptor().HasAccess(user, AccessLevel.Read);
        }
    }
}
